package com.burst.reptile.modular.zp.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.utils.textreview.TextContent;
import com.burst.reptile.framework.aspectj.lang.annotation.Msg;
import com.burst.reptile.framework.aspectj.lang.enums.MsgType;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.zp.entity.ZpComment;
import com.burst.reptile.modular.zp.service.IZpCommentService;
import com.burst.reptile.modular.zp.warpper.ZpCommentWrapper;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 评论列表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/zp/comment")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpCommentController extends BaseController{
	
	private final IZpCommentService commentService;

	/**
	 * 添加评论
	 * @return
	 */
	@PostMapping("/add")
	@Msg(businessType = MsgType.COMMENT)
	public Result<Object> add(@Validated @RequestBody ZpComment comment) {
		boolean examineText = TextContent.examineText(comment.getContent());
		if(examineText) {
			return Result.error("发布内容含敏感词汇");
		}
		return Result.success(commentService.add(comment));
	}
	
	/**
	 * 回复评论
	 * @return
	 */
	@PostMapping("/reply")
	public Result<Object> reply(@Validated @RequestBody ZpComment comment) {
		boolean examineText = TextContent.examineText(comment.getContent());
		if(examineText) {
			return Result.error("发布内容含敏感词汇");
		}
		return Result.success(commentService.reply(comment));
	}
	
	/**
	 * 评论列表
	 * @param order 排序 按热度 按时间 默认按热度
	 * @return
	 */
	@GetMapping("/list")
	public LayuiResult<Object> list(@RequestParam(required = false) String id, @RequestParam(required = false) String order) {
		Page<ZpComment> page = startPage();
		if(StrUtil.isEmpty(id)) {
			return LayuiResult.success(page.getTotal(), page.getRecords());
		}
		Page<ZpComment> pageList = commentService.pageList(page, id, order);
		new ZpCommentWrapper(pageList).wrap();
		return LayuiResult.success(pageList.getTotal(), pageList.getRecords());
	}
	
	/**
	 * 删除 评论或回复
	 * @param id ID
	 * @return
	 */
	@DeleteMapping("/del/{id}")
	public Result<Object> del(@PathVariable String id) {
		return Result.success(commentService.del(id));
	}
}
